﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Sausage.Common;

namespace Sausage.Web.Domain
{
    public static class Helper
    {
        #region User
        public static UserPrincipal CurrentPrincipal
        {
            get
            {
                if (HttpContext.Current.Request.IsAuthenticated)
                {
                    return (UserPrincipal)HttpContext.Current.User;
                }
                return null;
            }
        }

        public static bool IsAuthenticated
        {
            get
            {
                return HttpContext.Current.Request.IsAuthenticated;
            }
        }
        #endregion

        #region Plan
        public static int GetWeekOfYear(this DateTime time)
        {
            int weekNow = Convert.ToInt32(time.DayOfWeek);
            int dayDifferent = (-1) * (weekNow + 1); //今日与上周末的天数差
            int days = DateTime.Now.AddDays(dayDifferent).DayOfYear; //上周末是本年第几天
            int weeks = days / 7;

            if (days % 7 != 0)
                weeks++;

            return (weeks + 1);
        }

        public static void GetWeekRange(this DateTime time, out DateTime beginDateTime, out DateTime endDateTime)
        {
            time = new DateTime(time.Year, time.Month, time.Day);
            int weekNow = (int)time.DayOfWeek;
            int dayDifferent = (-1) * (weekNow); //今日与上周末的天数之差
            beginDateTime = time.AddDays(dayDifferent);
            endDateTime = time.AddDays(6 + dayDifferent);
        }

        public static bool IsPlanDelay(DateTime time)
        {
            DateTime beginDateTime, endDateTime;
            time.GetWeekRange(out beginDateTime, out endDateTime);

            DateTime nowBeginDateTime, nowEndDateTime;
            DateTime.Now.GetWeekRange(out nowBeginDateTime, out nowEndDateTime);

            return endDateTime < nowBeginDateTime.AddDays(7);
        }

        public static bool CouldSetPlan(DateTime time, out string errorMessage)
        {
            if (!IsAuthenticated)
            {
                errorMessage = "用户还未登陆！";
                return false;
            }

            DateTime beginDateTime, endDateTime;
            time.GetWeekRange(out beginDateTime, out endDateTime);

            DateTime nowBeginDateTime, nowEndDateTime;
            DateTime.Now.GetWeekRange(out nowBeginDateTime, out nowEndDateTime);

            if (beginDateTime > nowEndDateTime.AddDays(7))
            {
                errorMessage = "不可以超前提交，提交比系统日期超前的数据！";
                return false;
            }

            if (endDateTime < nowBeginDateTime.AddDays(7)) //the delay submit.
            {
                //check current user's delay submit period.
                if (
                    CurrentPrincipal.User.IsAllowDelay.HasValue &&
                    CurrentPrincipal.User.IsAllowDelay.Value &&
                    CurrentPrincipal.User.DelayExpiration.HasValue && DateTime.Now < CurrentPrincipal.User.DelayExpiration.Value &&
                    (!CurrentPrincipal.User.DelayBeginDate.HasValue || CurrentPrincipal.User.DelayBeginDate.Value <= time) &&
                    (!CurrentPrincipal.User.DelayEndDate.HasValue || CurrentPrincipal.User.DelayEndDate.Value >= time)
                )
                {
                    errorMessage = string.Empty;
                    return true;
                }

                errorMessage = "您所提交的数据已经超期，请向管理员申请延迟提交！";
                return false;
            }

            errorMessage = string.Empty;
            return true;
        }
        #endregion

        #region Item Common
        public static bool IsDelay(DateTime time)
        {
            //CouldDelayHours 点前 提交 比 今天早一天的数据
            return time < DateTime.Now.AddDays(-1).AddHours(-Utility.CouldDelayHours);
        }

        public static bool CouldSet(DateTime time, out string errorMessage)
        {
            if (!IsAuthenticated)
            {
                errorMessage = "用户还未登陆！";
                return false;
            }

            if (time > DateTime.Now) // because the ApplicationDate day is yyyy/MM/dd, in advance submit.
            {
                errorMessage = "不可以超前提交，提交比系统日期超前的数据！";
                return false;
            }

            if (IsDelay(time)) //the delay submit.
            {
                //check current user's delay submit period.
                if (
                    CurrentPrincipal.User.IsAllowDelay.HasValue &&
                    CurrentPrincipal.User.IsAllowDelay.Value &&
                    CurrentPrincipal.User.DelayExpiration.HasValue && DateTime.Now < CurrentPrincipal.User.DelayExpiration.Value &&
                    (!CurrentPrincipal.User.DelayBeginDate.HasValue || CurrentPrincipal.User.DelayBeginDate.Value <= time) &&
                    (!CurrentPrincipal.User.DelayEndDate.HasValue || CurrentPrincipal.User.DelayEndDate.Value >= time)
                )
                {
                    errorMessage = string.Empty;
                    return true;
                }

                errorMessage = "您所提交的数据已经超期，请向管理员申请延迟提交！";
                return false;
            }

            errorMessage = string.Empty;
            return true;
        }

        public static bool IsNeedOrganizationQuery(JosnPagingQueryModel query, out Guid targetID)
        {
            bool isWithChild = false;
            bool hasOrganizationID = false;
            bool hasIsWithChild = false;

            targetID = Guid.Empty;
            foreach (var item in query.FilterModel)
            {
                if (string.IsNullOrEmpty(item.value))
                    continue;

                if (string.Compare(item.property, "OrganizationID", true) == 0)
                {
                    targetID = Guid.Parse(item.value);
                    if (targetID != Guid.Empty)
                        hasOrganizationID = true;
                }

                if (string.Compare(item.property, "IsWithChild", true) == 0)
                {
                    hasIsWithChild = bool.TryParse(item.value, out isWithChild);
                }
            }

            //清除指定的过滤条件
            if (null != query.FilterModel && query.FilterModel.Length > 0)
            {
                //清除组织结构和是否包含子的过滤条件
                query.FilterModel = query.FilterModel.Where(item => string.Compare(item.property, "OrganizationID", true) != 0 && string.Compare(item.property, "IsWithChild", true) != 0).ToArray();
            }
            return hasOrganizationID && hasIsWithChild && isWithChild;
        }
        #endregion
    }
}
